ListView相信各位應該或多或少有使用過,是單層式,項目會一個個排列,而ExpandableListView(抽屜式列表)是雙層式,雖然外觀跟ListView相同,但可以透過點擊項目的方式做頁面的擴展與收縮。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ExpandableListView.ExpandableListViewTestActivity">
<ExpandableListView
android:id="@+id/expandableList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#00FFF7"
android:childDivider="#EF12FF"
android:dividerHeight="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
android:divider=
群組項目的分隔線android:childDivider=
子項目的分隔線android:dividerHeight=
分隔線的粗細,群組項目和子項目統一設定
2.expandable_first_list.xml;群組項目的版面配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/bigtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_weight="1"
android:textColor="#2B2BFF"
android:textSize="25sp"/>
</LinearLayout>
3.expandable_second_list.xml;子項目的版面配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF2121"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
extend BaseExpandableListAdapter可以alt+enter
就會將方法設定出來
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private ArrayList<String> dapartments;
private ArrayList<String> classes;
private LayoutInflater inflater;
public MyExpandableAdapter(Context context,ArrayList<String> dapartments,ArrayList<String>classes){
this.inflater=LayoutInflater.from(context);
this.dapartments=dapartments;
this.classes=classes;
}
@Override
public int getGroupCount() {//取得群組項目的數量
return dapartments.size();
}
@Override
public int getChildrenCount(int groupPosition) {//取得子項目的數量
return classes.size();
}
@Override
public Object getGroup(int groupPosition) {//取得群組項目的物件
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {//取得子項目的物件
return null;
}
@Override
public long getGroupId(int groupPosition) {////取得群組項目的Position ID
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {////取得子項目的Position ID
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null){//設置群組項目的view
convertView = inflater.inflate(R.layout.expandable_first_list,null);
}
convertView.setTag(R.layout.expandable_first_list,groupPosition);
convertView.setTag(R.layout.expandable_first_list,-1);
TextView textView = convertView.findViewById(R.id.bigtitle);
textView.setText(dapartments.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){//設置子項目的View
convertView = inflater.inflate(R.layout.expandable_second_list,null);
}
convertView.setTag(R.layout.expandable_second_list,groupPosition);
convertView.setTag(R.layout.expandable_second_list,-1);
TextView child1 = convertView.findViewById(R.id.title);
child1.setText(classes.get(childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {//定義子項目是否可以被點擊
return false;
}
}
public class ExpandableListViewTestActivity extends AppCompatActivity {
//初始化
private MyExpandableAdapter adapter;
private ExpandableListView expandableListView;
private ArrayList<String>dapartments=new ArrayList<>();//存入項目群組的資料
private ArrayList<String> classes = new ArrayList<>();//存入子項目的資料
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list_view_test);
setDate();
expandableListView=findViewById(R.id.expandableList);
//將資料傳入MyExpandableAdapter,expandableListView設定adapter
adapter=new MyExpandableAdapter(this,dapartments,classes);
expandableListView.setAdapter(adapter);
}
}
將科系跟班級存放資料
private void setDate() {
String[] department={"資料處理科","企業管理系","會計系"};
dapartments.add(department[0]);
dapartments.add(department[1]);
dapartments.add(department[2]);
String[] Studrnt1Class={"1年1班","1年2班","1年3班"};
classes.add(Studrnt1Class[0]);
classes.add(Studrnt1Class[1]);
classes.add(Studrnt1Class[2]);
}
這樣就完成了~
成圖: